<?php

namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Db;

class Find extends Controller {

    public function _initialize() {
        parent::_initialize();
        $this->view->engine->layout(false);
    }

    public function index() {

        return $this->fetch();
    }

    public function lists() {
        $param = input('param.');

        $where = 1;

        if(!$param['name'] and !$param['code']){
            $this->error('Name and certificate number must be filled in one');
        }
        
        if ($param['name']) {
            if (!strpos($param['name'], " ")) {
                $this->error('The name format is wrong');
            } else {
                if (count(explode(' ', trim($param['name']))) < 2) {
                    $this->error('The name format is wrong');
                }
                list($m, $x) = explode(' ', trim($param['name']));
                $where .= " and first_name='$m' and surnames='$x'";
            }
        }

        if ($param['code']) {
            $where .= " and certificate_code='{$param['code']}'";
        }
        if ($param['country']) {
            $where .= " and country='{$param['country']}'";
        }
        if ($param['city']) {
            $where .= " and city='{$param['city']}'";
        }

        $data = Db::name('member')->where($where)->select();

        return $this->fetch('', ['data' => $data]);
    }

    public function info() {
        $id = input('param.id');

        $data = Db::name('member')->where(['id' => $id])->find();

        return $this->fetch('', ['data' => $data]);
    }

}
